package com.leaf.lowcode.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.leaf.lowcode.API.DoorFunctionApi;
import com.leaf.lowcode.entity.Dooreven;
import com.leaf.lowcode.entity.DoorevenDTO;
import com.leaf.lowcode.entity.door_control.EventsRequest;
import com.leaf.lowcode.mapper.DoorevenMapper;
import com.leaf.lowcode.mapper.DoorlistMapper;
import com.leaf.lowcode.utils.RegularInstance;
import com.leaf.lowcode.utils.jsontobean.JsonToBeanV;
import com.tencent.welink.iot.SubDeviceDTO;
import com.tencent.welink.iot.WelinkHttp;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.*;


/**
 * 门禁通行事件
 *   event.put("orgname", doorevenDTO.getOrgName());
 *   event.put("doorname", doorevenDTO.getDoorName());
 *   event.put("owndoor",doorevenDTO.getDevName());
 *   event.put("indecodex",doorevenDTO.getDoorIndexCode());
 */


@Component
public class DoorEvent {
    @Autowired
    private DoorevenMapper doorevenMapper;

    @Autowired
    private DoorlistMapper doorlistMapper;

   @Scheduled(initialDelay = 3 * 1000, fixedRate = 10 * 1000)
    public void execute() {
        List<SubDeviceDTO> subDeviceDTOList = RegularInstance.subDeviceDTOList;
        List<String> indexCodes = doorlistMapper.selectdoor();
        for (int j = 0; j < indexCodes.size(); j++) {
           // System.out.println(indexCodes.get(j));
            DoorFunctionApi doorFunctionApi = new DoorFunctionApi();
            EventsRequest eventsRequest = new EventsRequest();
            eventsRequest.setPageNo(1);
            eventsRequest.setPageSize(2);
            ArrayList<String> indexcodList=new ArrayList<String>();
            indexcodList.add(indexCodes.get(j));
            eventsRequest.setDoorIndexCodes(indexcodList);
            String doorcount = doorFunctionApi.events(eventsRequest);
            JSONObject jsonObject = JSONObject.parseObject(doorcount);
            JSONArray list = (JSONArray) ((JSONObject) jsonObject.get("data")).get("list");
            Dooreven dooreven = new Dooreven();
            DoorevenDTO doorevenDTO = new DoorevenDTO();
            for (int i = 0; i < list.size(); i++) {
                JSONObject object = (JSONObject) list.get(i);
                JsonToBeanV<Dooreven> jsonToBeanV = new JsonToBeanV<Dooreven>();
                dooreven = jsonToBeanV.tansfrom(dooreven, object);
                BeanUtils.copyProperties(dooreven, doorevenDTO);
                doorevenDTO.setOrgName(doorevenDTO.getOrgName().replaceFirst("默认组织/", ""));
                if (subDeviceDTOList != null) {
                    for (SubDeviceDTO dto : subDeviceDTOList) {
                        String pid = dto.getPid();
                        String sn = dto.getSn();
                        String din = dto.getDin();
                        if (doorevenDTO.getDoorIndexCode().equals(sn)) {
                            System.out.println("门禁事件");
                            System.out.println("door"+doorevenDTO.toString());
                            //  System.out.println("======================门禁卡========================"+readerdoorstatesend.getIndexCode());
                            JSONObject standardData = new JSONObject();
                            standardData.put("reportTs", System.currentTimeMillis());
                            JSONObject profile = new JSONObject();
                            profile.put("modelId", 200729);
                            profile.put("poiCode", "w0703003");
                            profile.put("appType","access_control");
                            standardData.put("profile", profile);
                            WelinkHttp wh = new WelinkHttp(RegularInstance.registerData1, RegularInstance.loginData);
                            JSONObject event = new JSONObject();
                            JSONObject events = new JSONObject();
                            event.put("eventType", 1);
                            event.put("eventTs", System.currentTimeMillis());
                            event.put("describe", "");
                            event.put("name", doorevenDTO.getPersonName());
                            event.put("time", doorevenDTO.getEventTime().toString().substring(11,19));
                            System.out.println(doorevenDTO.getEventTime().toString().substring(11,19));
                            event.put("orgname", doorevenDTO.getOrgName());
                            event.put("doorname", doorevenDTO.getDoorName());
                            event.put("owndoor",doorevenDTO.getDevName());
                            event.put("indecodex",din);
                            event.put("count",1);
                         //   event.put("doorinfo",propertie);
                            /**
                             *
                             */
                            events.put("door_in",event);
                            standardData.put("events",events);
                            standardData.put("properties", new JSONObject());
                            //standardData.put("properties", "{}");
                            try {
                                System.out.println("上传数据");
                                String devRunEvent = JSONObject.toJSONString(standardData, SerializerFeature.WriteMapNullValue);
                                System.out.println("发送设备运行时事件数据==" + devRunEvent);
                                System.out.println("--------------------------------开始传输设备运行时事件数据");
                                wh.sendMessage(RegularInstance.loginData.load(), 80000, Long.parseLong(din), devRunEvent, true);
                                System.out.println("上传成功");
                            } catch (Exception e) {
                                e.printStackTrace();
                                System.out.println("设备运行时事件数据传输出现异常");
                            }
                        }
                    }
                }
            }
        }
    }
}


